package com.lan.test01;

import com.lan.domain.Student;

import java.lang.reflect.Field;

public class Test04 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
        //1.获取Class对象
        Class clazz = Class.forName("com.lan.domain.Student");

        //2.获取name这个Field对象
        Field fname = clazz.getDeclaredField("name");

        //3.取消一下访问检查，暴力反射
        fname.setAccessible(true);

        //4.创建对象
        Student stu = (Student) clazz.newInstance();

        //5.给指定对象赋值
        fname.set(stu,"张三");

        //6.获取指定对象的name值
        System.out.println(fname.get(stu));
    }
}
